﻿Imports System.Net.NetworkInformation
'Imports System.Web.UI.WebControls.Expressions
Module modCommon
    Public Function getMsgStr(ByVal msgId As String, Optional ByVal args1 As String = "", Optional ByVal args2 As String = "", Optional ByVal args3 As String = "") As String

        If String.IsNullOrEmpty(args1) AndAlso String.IsNullOrEmpty(args2) AndAlso String.IsNullOrEmpty(args3) Then
            getMsgStr = htMsgMst(msgId)
            Exit Function

        ElseIf Not String.IsNullOrEmpty(args1) AndAlso Not String.IsNullOrEmpty(args2) AndAlso String.IsNullOrEmpty(args3) Then
            getMsgStr = String.Format(htMsgMst(msgId), args1, args2)
            Exit Function
        ElseIf Not String.IsNullOrEmpty(args1) AndAlso Not String.IsNullOrEmpty(args2) AndAlso Not String.IsNullOrEmpty(args3) Then
            getMsgStr = String.Format(htMsgMst(msgId), args1, args2, args3)
            Exit Function
        Else
            getMsgStr = String.Format(htMsgMst(msgId), args1, args2)
            Exit Function
        End If

    End Function
    ''' <summary>
    ''' 检查是否为空
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <returns></returns>
    Public Function chkEmpty(Sender As Object) As Boolean
        If Sender.text = "" Then
            MsgBox(getMsgStr("msg001", Sender.tag))
            Sender.Select()
            Return False
        End If
        Return True
    End Function
    ''' <summary>
    ''' 检查是否为空
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <returns></returns>
    Public Function chkZero(Sender As Object) As Boolean
        If Sender.text = "0" Then
            MsgBox(getMsgStr("msg002", Sender.tag))
            Sender.Select()
            Return False
        End If
        Return True
    End Function

    ''' <summary>
    ''' 检查是否为负数
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <returns></returns>
    Public Function chkNegative(Sender As Object) As Boolean
        If CInt(Sender.text) < 0 Then
            MsgBox(getMsgStr("msg041", Sender.tag))
            Sender.Select()
            Return False
        End If
        Return True
    End Function

    Public Function chkIP(Sender As Object) As Boolean
        '检查IP地址是否合法函数
        Dim intLoop As Integer
        Dim arrIP
        Dim rtn As Boolean = True '函数初始值为true 
        '将输入的IP用"."分割为数组，数组下标从0开始，所以有效IP分割后的数组上界必须为3   
        arrIP = Split(Sender.text, ".")
        If UBound(arrIP) <> 3 Then
            rtn = False
        Else
            For intLoop = 0 To UBound(arrIP)
                '检查数组元素中各项是否为数字，如果不是则不是有效IP 
                If Not IsNumeric(arrIP(intLoop)) Then
                    rtn = False
                Else
                    '检查IP数字是否满足IP的取值范围 
                    If arrIP(intLoop) > 255 Or arrIP(intLoop) < 0 Then
                        rtn = False
                    End If
                End If
            Next
        End If
        If rtn = False Then
            MsgBox(getMsgStr("msg003", Sender.tag))
            Sender.Select()

        End If
        Return rtn
    End Function
    ''' <summary>
    ''' 检查输入位数
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <param name="max">最大位数</param>
    ''' <returns></returns>
    Public Function chkRange(Sender As Object, max As Integer) As Boolean
        Dim rtn As Boolean = True '函数初始值为true 
        Dim lenth As Integer = 0
        lenth = Len(Sender.Text)
        If lenth > max Then
            rtn = False
            MsgBox(getMsgStr("msg010", Sender.tag, max))
        End If
        Return rtn
    End Function
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <returns></returns>
    Public Function chkWtRange(Sender As Object) As Boolean
        Dim rtn As Boolean = True '函数初始值为true 
        If Sender.Text <> "" Then
            If Sender.Text > 9999.99 OrElse Sender.Text < -999.99 Then
                rtn = False
                MsgBox(getMsgStr("msg010", Sender.tag, 6))
            End If
        End If

        Return rtn
    End Function
    ''' <summary>
    ''' 检查是否大于0
    ''' </summary>
    ''' <param name="Sender">要检查的控件</param>
    ''' <returns></returns>
    Public Function chkEmptyNum(Sender As Object) As Boolean
        If Val(Sender.text) < 1 Then
            MsgBox(getMsgStr("msg002", Sender.tag))
            Sender.Select()
            Return False
        End If
        Return True
    End Function

    Public Function getClassesName(_id As String) As String
        Dim rtn As String = ""
        Select Case _id
            Case "1"
                rtn = "早班"
            Case "2"
                rtn = "中班"
            Case "3"
                rtn = "晚班"
        End Select
        Return rtn
    End Function
    Public Function getClassesID(_name As String) As String
        Dim rtn As String = ""
        Select Case _name
            Case "早班"
                rtn = "1"
            Case "中班"
                rtn = "2"
            Case "晚班"
                rtn = "3"
        End Select
        Return rtn
    End Function

    Public Function getWorkshopID(workshopName As String)
        Dim rtn As String = ""

        Select Case workshopName
            Case "骨架板车间"
                rtn = "GJB"
            Case "胶片车间"
                rtn = "JP"
            Case "硫化车间"
                rtn = "LH"
        End Select
        Return rtn

    End Function

    Public Function getWorkshopName(workshopID As String)
        Dim rtn As String = ""

        Select Case workshopID
            Case "GJB"
                rtn = "骨架板车间"
            Case "JP"
                rtn = "胶片车间"
            Case "LH"
                rtn = "硫化车间"
        End Select
        Return rtn

    End Function

    Public Function DBNULLtoBlank(obj As Object) As String
        Dim rtn As String = ""
        If IsDBNull(obj) Then
            rtn = ""
        Else
            rtn = obj.ToString
        End If
        Return rtn
    End Function
    Public Function DBNULLtoZero(obj As Object) As String
        Dim rtn As String = ""
        Try
            If IsDBNull(obj) Then
                rtn = "0"
            Else
                rtn = obj.ToString
            End If
        Catch ex As InvalidCastException
            rtn = "0"
        End Try

        Return rtn
    End Function
    Public Function DBNULLtoDate(obj As Object) As Object
        Dim rtn As String = ""
        If IsDBNull(obj) Then
            rtn = ""
        Else
            rtn = CDate(obj)
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' 自增序列查询
    ''' </summary>
    ''' <param name="key"></param>
    ''' <returns>自增ID</returns>
    Friend Function getNextKeyNo(_type_id As String, key As String) As String
        Dim szSql As String = ""
        szSql = "SELECT no_value"
        szSql &= " FROM sf.t_auto_number WHERE no_key = ? and record_date =? and type_id=?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, key, Now.ToString("yyyyMMdd"), _type_id)
        If IsNothing(dt) Then
            getNextKeyNo = "0001"
        Else
            Dim i As Integer
            If dt.Rows.Count = 0 Then
                i = 1
            Else
                i = CInt(dt.Rows(0)("no_value")) + 1
            End If
            getNextKeyNo = i.ToString("0000")
        End If

    End Function

    Public Sub setNextKeyNo(_type_id As String, key As String)
        Dim szSql As String
        szSql = String.Empty
        If CInt(getNextKeyNo(_type_id, key)) = 1 Then
            szSql &= "INSERT INTO sf.t_auto_number(no_key, no_value, record_date, type_id) VALUES (?, ?,?,?)"
            DBacs.ExecuteUpdateSql(szSql, -1, key, getNextKeyNo(_type_id, key), Now.ToString("yyyyMMdd"), _type_id)

        Else
            szSql &= "update sf.t_auto_number set no_value = ? , record_date =?, type_id = ?  where no_key = ?"
            DBacs.ExecuteUpdateSql(szSql, -1, getNextKeyNo(_type_id, key), Now.ToString("yyyyMMdd"), _type_id, key)

        End If


    End Sub

    Public Sub setAllErrMsg()
        'メッセージが取得済みの場合は設定しない
        If htMsgMst.Count = 0 Then
            Dim objmsg As New clsMessage
            Dim dtMsgMst As DataTable = objmsg.GetAllMsg()
            For Each dr In dtMsgMst.Rows
                With htMsgMst
                    .Add(dr("msg_id"), dr("msg"))
                End With
            Next
        End If
    End Sub


    'Public Sub chklst_Click(sender As Object, e As EventArgs)
    '    Dim item As New DevExpress.XtraEditors.Controls.CheckedListBoxItem
    '    item = CType(sender, DevExpress.XtraEditors.CheckedListBoxControl).SelectedItem
    '    If item.CheckState = CheckState.Unchecked Then
    '        item.CheckState = CheckState.Checked
    '    Else
    '        item.CheckState = CheckState.Unchecked
    '    End If
    'End Sub

    'Public Sub getCurrentProdInstInfo()
    '    Dim objProdInst As New clsProdInst
    '    Dim objPGDList As New clsPgdList
    '    Dim objProdSpec As New clsProdSpec
    '    Dim PGD_ID As String = ""
    '    PGD_ID = objProdInst.getCurrentPGD_ID(g_Current_WorkShop, g_Current_Classes, Now.Date.ToString)
    '    If IsNothing(PGD_ID) Then Exit Sub
    '    Dim row As DataRow = Nothing
    '    Dim pgdid As String = ""

    '    If IsNothing(PGD_ID) = False Then

    '        Dim _dt As DataTable = objPGDList.reload(PGD_ID)
    '        If _dt.Rows.Count > 0 Then
    '            Dim _SpecDt As DataTable
    '            g_Current_PGD_ID = _dt.Rows(0)("pgd_id")
    '            g_Current_PJ_Name = _dt.Rows(0)("pj_name")
    '            g_Current_PROD_SPEC = _dt.Rows(0)("prod_spec")
    '            _SpecDt = objProdSpec.selectByProdSpecId(g_Current_PROD_SPEC)
    '            If _SpecDt.Rows.Count > 0 Then
    '                g_Current_JP_Type = _SpecDt.Rows(0)("jp_type")
    '                g_Current_FLB_Type = _SpecDt.Rows(0)("flb_type")
    '                g_Current_GJB_Type = _SpecDt.Rows(0)("gjb_type")
    '            End If
    '        End If

    '    End If

    'End Sub


    Public Function GetIPAddr() As String
        ' アダプタリストを取得する
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        GetIPAddr = String.Empty
        For Each adapter As NetworkInterface In adapters

            ' ネットワーク接続状態が UP のアダプタのみ表示 
            If (adapter.OperationalStatus = OperationalStatus.Up) Then

                Dim ip_prop As IPInterfaceProperties = adapter.GetIPProperties()

                ' ユニキャスト IPv4 アドレスの取得
                For Each addr As IPAddressInformation In ip_prop.UnicastAddresses()
                    If addr.Address.IsIPv6LinkLocal OrElse
                       addr.Address.IsIPv6Multicast OrElse
                       addr.Address.IsIPv6SiteLocal OrElse
                       addr.Address.IsIPv6Teredo Then
                        Continue For
                    End If

                    If addr.Address.ToString.Length > 16 Then
                        Continue For
                    End If

                    GetIPAddr = addr.Address.ToString
                    Exit Function
                Next

            End If
        Next

        If GetIPAddr.Length <= 0 Then
            Return ""
        End If
    End Function

    Public Function getSysParam(_param_key As String) As String
        Dim rtn As String = ""
        Dim szSql As String = ""
        szSql = "SELECT param_value FROM sf.t_sys_param "
        szSql &= "WHERE param_key=?"
        Dim dtsys As DataTable = DBacs.ExecuteSql(szSql, -1, _param_key)
        If dtsys.Rows.Count > 0 Then
            rtn = dtsys(0)(0)
        End If
        Return rtn
    End Function
    Public Function setSysParam(_param_key As String, _param_value As String) As Integer
        Dim rtn As String = ""
        Dim szSql As String = ""
        szSql = "update sf.t_sys_param "
        szSql &= "set param_value=?"
        szSql &= "WHERE param_key=?"
        Return DBacs.ExecuteUpdateSql(szSql, -1, _param_value, _param_key)
    End Function
End Module
